import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**客户端战斗特效图标属性枚举*/
export const enum E_ClientBattleEffectIcon {
	/**buff id*/
	EFFECT_ID = 0,
	/**图标*/
	ICON = 1,
	/**角色id*/
	ACTOR_ID = 2,
	/**最近一次生效时间*/
	ACTIVE_TIME = 3,
	/**产生时间*/
	CREATE_TIME = 4,
	/**结束时间*/
	END_TIME = 5,
	/**配置id*/
	CONFIG_ID = 6,
	/**枚举数量*/
	EnumCount = 7
}


/**客户端战斗特效图标*/
export class ClientBattleEffectIcon {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_CLIENT_BATTLE_EFFECT_ICON_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_CLIENT_BATTLE_EFFECT_ICON_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: ClientBattleEffectIcon[] = [];

	/**创建入口*/
	public static create(json?: string): ClientBattleEffectIcon {
		const c = this.POOL.pop() || new ClientBattleEffectIcon();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**buff id (uint32)*/
	protected _effectId: number;
	/**图标 (int32)*/
	protected _icon: number;
	/**角色id (uint16)*/
	protected _actorId: number;
	/**最近一次生效时间 (uint32)*/
	protected _activeTime: number;
	/**产生时间 (uint32)*/
	protected _createTime: number;
	/**结束时间 (uint32)*/
	protected _endTime: number;
	/**配置id (uint32)*/
	protected _configId: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (ClientBattleEffectIcon.POOL.indexOf(this) === -1) {
			ClientBattleEffectIcon.POOL.push(this);
		}
	}

	/**buff id*/
	public get effectId() {
		return this._effectId;
	}


	/**buff id*/
	public set effectId(v: number) {
		this._effectId = v;
	}


	/**图标*/
	public get icon() {
		return this._icon;
	}


	/**图标*/
	public set icon(v: number) {
		this._icon = v;
	}


	/**角色id*/
	public get actorId() {
		return this._actorId;
	}


	/**角色id*/
	public set actorId(v: number) {
		this._actorId = v;
	}


	/**最近一次生效时间*/
	public get activeTime() {
		return this._activeTime;
	}


	/**最近一次生效时间*/
	public set activeTime(v: number) {
		this._activeTime = v;
	}


	/**产生时间*/
	public get createTime() {
		return this._createTime;
	}


	/**产生时间*/
	public set createTime(v: number) {
		this._createTime = v;
	}


	/**结束时间*/
	public get endTime() {
		return this._endTime;
	}


	/**结束时间*/
	public set endTime(v: number) {
		this._endTime = v;
	}


	/**配置id*/
	public get configId() {
		return this._configId;
	}


	/**配置id*/
	public set configId(v: number) {
		this._configId = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_ClientBattleEffectIcon): any {
		switch (k) {
			case E_ClientBattleEffectIcon.EFFECT_ID: return this._effectId;
			case E_ClientBattleEffectIcon.ICON: return this._icon;
			case E_ClientBattleEffectIcon.ACTOR_ID: return this._actorId;
			case E_ClientBattleEffectIcon.ACTIVE_TIME: return this._activeTime;
			case E_ClientBattleEffectIcon.CREATE_TIME: return this._createTime;
			case E_ClientBattleEffectIcon.END_TIME: return this._endTime;
			case E_ClientBattleEffectIcon.CONFIG_ID: return this._configId;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_ClientBattleEffectIcon, v: any): void {
		switch (k) {
			case E_ClientBattleEffectIcon.EFFECT_ID: this._effectId = v; break;
			case E_ClientBattleEffectIcon.ICON: this._icon = v; break;
			case E_ClientBattleEffectIcon.ACTOR_ID: this._actorId = v; break;
			case E_ClientBattleEffectIcon.ACTIVE_TIME: this._activeTime = v; break;
			case E_ClientBattleEffectIcon.CREATE_TIME: this._createTime = v; break;
			case E_ClientBattleEffectIcon.END_TIME: this._endTime = v; break;
			case E_ClientBattleEffectIcon.CONFIG_ID: this._configId = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//buff id
		this._effectId = 0;
		//图标
		this._icon = 0;
		//角色id
		this._actorId = 0;
		//最近一次生效时间
		this._activeTime = 0;
		//产生时间
		this._createTime = 0;
		//结束时间
		this._endTime = 0;
		//配置id
		this._configId = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._effectId) {
			json.effectId = this._effectId;
		}
		if (this._icon) {
			json.icon = this._icon;
		}
		if (this._actorId) {
			json.actorId = this._actorId;
		}
		if (this._activeTime) {
			json.activeTime = this._activeTime;
		}
		if (this._createTime) {
			json.createTime = this._createTime;
		}
		if (this._endTime) {
			json.endTime = this._endTime;
		}
		if (this._configId) {
			json.configId = this._configId;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.effectId) {
			this._effectId = json.effectId;
		}
		if (json.icon) {
			this._icon = json.icon;
		}
		if (json.actorId) {
			this._actorId = json.actorId;
		}
		if (json.activeTime) {
			this._activeTime = json.activeTime;
		}
		if (json.createTime) {
			this._createTime = json.createTime;
		}
		if (json.endTime) {
			this._endTime = json.endTime;
		}
		if (json.configId) {
			this._configId = json.configId;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeUint32(this._effectId);
		byte.writeInt32(this._icon);
		byte.writeUint16(this._actorId);
		byte.writeUint32(this._activeTime);
		byte.writeUint32(this._createTime);
		byte.writeUint32(this._endTime);
		byte.writeUint32(this._configId);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._effectId = byte.readUint32();
		this._icon = byte.readInt32();
		this._actorId = byte.readUint16();
		this._activeTime = byte.readUint32();
		this._createTime = byte.readUint32();
		this._endTime = byte.readUint32();
		this._configId = byte.readUint32();
		return this;
	}


	/**克隆*/
	public clone(): ClientBattleEffectIcon {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = ClientBattleEffectIcon.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = ClientBattleEffectIcon.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//buff id
			if (idx === E_ClientBattleEffectIcon.EFFECT_ID) {
				const oldv = this._effectId;
				this._effectId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._effectId, oldv);
				i += 2;
				continue;
			}
			//图标
			if (idx === E_ClientBattleEffectIcon.ICON) {
				const oldv = this._icon;
				this._icon = us[i + 1];
				IAsync.dispatch(eventID, idx, this._icon, oldv);
				i += 2;
				continue;
			}
			//角色id
			if (idx === E_ClientBattleEffectIcon.ACTOR_ID) {
				const oldv = this._actorId;
				this._actorId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._actorId, oldv);
				i += 2;
				continue;
			}
			//最近一次生效时间
			if (idx === E_ClientBattleEffectIcon.ACTIVE_TIME) {
				const oldv = this._activeTime;
				this._activeTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._activeTime, oldv);
				i += 2;
				continue;
			}
			//产生时间
			if (idx === E_ClientBattleEffectIcon.CREATE_TIME) {
				const oldv = this._createTime;
				this._createTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._createTime, oldv);
				i += 2;
				continue;
			}
			//结束时间
			if (idx === E_ClientBattleEffectIcon.END_TIME) {
				const oldv = this._endTime;
				this._endTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._endTime, oldv);
				i += 2;
				continue;
			}
			//配置id
			if (idx === E_ClientBattleEffectIcon.CONFIG_ID) {
				const oldv = this._configId;
				this._configId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._configId, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in ClientBattleEffectIcon with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: ClientBattleEffectIcon): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._effectId !== other._effectId) {
			updates.push(E_ClientBattleEffectIcon.EFFECT_ID, this._effectId);
		}
		if (this._icon !== other._icon) {
			updates.push(E_ClientBattleEffectIcon.ICON, this._icon);
		}
		if (this._actorId !== other._actorId) {
			updates.push(E_ClientBattleEffectIcon.ACTOR_ID, this._actorId);
		}
		if (this._activeTime !== other._activeTime) {
			updates.push(E_ClientBattleEffectIcon.ACTIVE_TIME, this._activeTime);
		}
		if (this._createTime !== other._createTime) {
			updates.push(E_ClientBattleEffectIcon.CREATE_TIME, this._createTime);
		}
		if (this._endTime !== other._endTime) {
			updates.push(E_ClientBattleEffectIcon.END_TIME, this._endTime);
		}
		if (this._configId !== other._configId) {
			updates.push(E_ClientBattleEffectIcon.CONFIG_ID, this._configId);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(ClientBattleEffectIcon.TOTAL_EVENT_ID, this.getV(0));
	}

}
